package com.kenyi.co.ui.update;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kenyi.co.R;
import com.kenyi.co.ui.home.bean.SysVersionBean;

public class UpdatePopWindow extends PopupWindow implements View.OnClickListener {
    private final View view;
    private Activity context;
    private ImageView iv_update_close;
    private String updateinfo;
    private SysVersionBean packlist;
    private Button bt_update;
    private TextView tv_update_update_content, tv_update_version;

    public UpdatePopWindow(Activity context, String updateinfo) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.layout_update_recylerview, null);//alt+ctrl+f
        this.context = context;
        this.updateinfo = updateinfo;
        Gson gson = new Gson();
        packlist = gson.fromJson(updateinfo, SysVersionBean.class);

        initView();
        initPopWindow();

    }

    private void initView() {
        iv_update_close = view.findViewById(R.id.iv_update_close);
        tv_update_update_content = view.findViewById(R.id.tv_update_update_content);
        tv_update_version = view.findViewById(R.id.tv_update_version);
        bt_update = view.findViewById(R.id.bt_update);
        iv_update_close.setOnClickListener(this);
        bt_update.setOnClickListener(this);
        tv_update_update_content.setText(packlist.getDatas().getDesc());
        tv_update_version.setText(packlist.getDatas().getVersion());
        if(packlist.getDatas().isMandatory()){
            iv_update_close.setVisibility(View.GONE);
        }

    }

    /**
     * 设置添加屏幕的背景透明度(值越大,透明度越高)
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    private void initPopWindow() {
        this.setContentView(view);
        // 设置弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);

        // 设置弹出窗体可点击()
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new BitmapDrawable());
        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00FFFFFF);
        //设置弹出窗体的背景
        this.setBackgroundDrawable(dw);
//        backgroundAlpha(context, 0.6f);//0.0-1.0

    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }


    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if (Build.VERSION.SDK_INT >= 24) {

            Rect rect = new Rect();
            anchor.getWindowVisibleDisplayFrame(rect);
            Activity activity = (Activity) anchor.getContext();
            Rect outRect1 = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
            int h = outRect1.height() - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {


            case R.id.bt_update:

                launchAppDetail(context.getPackageName());

                break;
            case R.id.iv_update_close:
                dismiss();
                break;
            default:
                break;
        }
    }

    public void launchAppDetail(String appPkg) {
        try {
            if (TextUtils.isEmpty(appPkg)) return;
            Uri uri = Uri.parse("market://details?id=" + appPkg);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
